package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
public class EmpController {
    @Autowired
    private EmpService empService;




    @GetMapping("/emps")
    public Result findPage(String name, Integer gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                           @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5")Integer pageSize){
        LimitConditons limitConditons = new LimitConditons(name.trim(), gender, begin, end, page, pageSize);
        PageBean pageBean=empService.findPage(limitConditons);
        return Result.success(pageBean);
    }
    @DeleteMapping("/emps/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        empService.deleteByIds(ids);

        return Result.success();
    }
    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp){
        empService.add(emp);
        return Result.success();
    }

    @GetMapping("/emps/{id}")
    public Result findById(@PathVariable Integer id){
        Emp emp=empService.findById(id);
        return Result.success(emp);
    }

    @PutMapping("/emps")

    private Result updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        return empService.login(emp);
    }
}
